# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# https://arxiv.org/abs/2007.10985

import MinkowskiEngine.MinkowskiOps as me
import torch
import torch.nn as nn
from MinkowskiEngine import (
    MinkowskiPoolingTranspose,
    MinkowskiReLU,
    MinkowskiSigmoid,
    MinkowskiSumPooling,
    SparseTensor,
)

from model.modules.common import ConvType, NormType, conv, conv_tr
from model.modules.resnet_block import BasicBlock
from model.resnet import ResNetBase, get_norm


class Res16UNetBase(ResNetBase):
    BLOCK = None
    PLANES = (32, 64, 128, 256, 256, 256, 256, 256)
    DILATIONS = (1, 1, 1, 1, 1, 1, 1, 1)
    LAYERS = (2, 2, 2, 2, 2, 2, 2, 2)
    INIT_DIM = 32
    OUT_PIXEL_DIST = 1
    NORM_TYPE = NormType.BATCH_NORM
    NON_BLOCK_CONV_TYPE = ConvType.SPATIAL_HYPERCUBE
    # CONV_TYPE = ConvType.SPATIAL_HYPERCUBE_TEMPORAL_HYPERCROSS
    CONV_TYPE = ConvType.SPATIAL_HYPERCUBE  # FOR ME0.5

    def __init__(self, in_channels, out_channels, config, D=3):
        self.normalize_feature = config["normalize_features"]
        super(Res16UNetBase, self).__init__(in_channels, out_channels, config, D)

    def network_initialization(self, in_channels, out_channels, config, D):
        dilations = self.DILATIONS
        bn_momentum = config["bn_momentum"]

        def space_n_time_m(n, m):
            return n if D == 3 else [n, n, n, m]

        if D == 4:
            self.OUT_PIXEL_DIST = space_n_time_m(self.OUT_PIXEL_DIST, 1)

        self.inplanes = self.INIT_DIM  # channel number of convolution layers
        self.conv0p1s1 = conv(
            in_channels,
            self.inplanes,
            kernel_size=space_n_time_m(config["kernel_size"], 1),
            stride=1,
            dilation=1,
            conv_type=self.NON_BLOCK_CONV_TYPE,
            D=D,
        )

        self.bn0 = get_norm(
            self.NORM_TYPE, self.inplanes, D, bn_momentum=bn_momentum
        )  # always choose batch normalization

        self.conv1p1s2 = conv(
            self.inplanes,
            self.inplanes,
            kernel_size=space_n_time_m(2, 1),
            stride=space_n_time_m(2, 1),
            dilation=1,
            conv_type=self.NON_BLOCK_CONV_TYPE,
            D=D,
        )  # kernel_size=2, stride=2
        self.bn1 = get_norm(self.NORM_TYPE, self.inplanes, D, bn_momentum=bn_momentum)
        self.block1 = self._make_layer(
            self.BLOCK,
            self.PLANES[0],
            self.LAYERS[0],
            dilation=dilations[0],
            norm_type=self.NORM_TYPE,
            bn_momentum=bn_momentum,
        )

        self.conv2p2s2 = conv(
            self.inplanes,
            self.inplanes,
            kernel_size=space_n_time_m(2, 1),
            stride=space_n_time_m(2, 1),
            dilation=1,
            conv_type=self.NON_BLOCK_CONV_TYPE,
            D=D,
        )
        self.bn2 = get_norm(self.NORM_TYPE, self.inplanes, D, bn_momentum=bn_momentum)
        self.block2 = self._make_layer(
            self.BLOCK,
            self.PLANES[1],
            self.LAYERS[1],
            dilation=dilations[1],
            norm_type=self.NORM_TYPE,
            bn_momentum=bn_momentum,
        )

        self.conv3p4s2 = conv(
            self.inplanes,
            self.inplanes,
            kernel_size=space_n_time_m(2, 1),
            stride=space_n_time_m(2, 1),
            dilation=1,
            conv_type=self.NON_BLOCK_CONV_TYPE,
            D=D,
        )
        self.bn3 = get_norm(self.NORM_TYPE, self.inplanes, D, bn_momentum=bn_momentum)
        self.block3 = self._make_layer(
            self.BLOCK,
            self.PLANES[2],
            self.LAYERS[2],
            dilation=dilations[2],
            norm_type=self.NORM_TYPE,
            bn_momentum=bn_momentum,
        )

        self.conv4p8s2 = conv(
            self.inplanes,
            self.inplanes,
            kernel_size=space_n_time_m(2, 1),
            stride=space_n_time_m(2, 1),
            dilation=1,
            conv_type=self.NON_BLOCK_CONV_TYPE,
            D=D,
        )
        self.bn4 = get_norm(self.NORM_TYPE, self.inplanes, D, bn_momentum=bn_momentum)
        self.block4 = self._make_layer(
            self.BLOCK,
            self.PLANES[3],
            self.LAYERS[3],
            dilation=dilations[3],
            norm_type=self.NORM_TYPE,
            bn_momentum=bn_momentum,
        )
        self.convtr4p16s2 = conv_tr(
            self.inplanes,
            self.PLANES[4],
            kernel_size=space_n_time_m(2, 1),
            upsample_stride=space_n_time_m(2, 1),
            dilation=1,
            bias=False,
            conv_type=self.NON_BLOCK_CONV_TYPE,
            D=D,
        )
        self.bntr4 = get_norm(
            self.NORM_TYPE, self.PLANES[4], D, bn_momentum=bn_momentum
        )

        self.inplanes = self.PLANES[4] + self.PLANES[2] * self.BLOCK.expansion
        self.block5 = self._make_layer(
            self.BLOCK,
            self.PLANES[4],
            self.LAYERS[4],
            dilation=dilations[4],
            norm_type=self.NORM_TYPE,
            bn_momentum=bn_momentum,
        )
        self.convtr5p8s2 = conv_tr(
            self.inplanes,
            self.PLANES[5],
            kernel_size=space_n_time_m(2, 1),
            upsample_stride=space_n_time_m(2, 1),
            dilation=1,
            bias=False,
            conv_type=self.NON_BLOCK_CONV_TYPE,
            D=D,
        )
        self.bntr5 = get_norm(
            self.NORM_TYPE, self.PLANES[5], D, bn_momentum=bn_momentum
        )

        self.inplanes = self.PLANES[5] + self.PLANES[1] * self.BLOCK.expansion
        self.block6 = self._make_layer(
            self.BLOCK,
            self.PLANES[5],
            self.LAYERS[5],
            dilation=dilations[5],
            norm_type=self.NORM_TYPE,
            bn_momentum=bn_momentum,
        )
        self.convtr6p4s2 = conv_tr(
            self.inplanes,
            self.PLANES[6],
            kernel_size=space_n_time_m(2, 1),
            upsample_stride=space_n_time_m(2, 1),
            dilation=1,
            bias=False,
            conv_type=self.NON_BLOCK_CONV_TYPE,
            D=D,
        )
        self.bntr6 = get_norm(
            self.NORM_TYPE, self.PLANES[6], D, bn_momentum=bn_momentum
        )

        self.inplanes = self.PLANES[6] + self.PLANES[0] * self.BLOCK.expansion
        self.block7 = self._make_layer(
            self.BLOCK,
            self.PLANES[6],
            self.LAYERS[6],
            dilation=dilations[6],
            norm_type=self.NORM_TYPE,
            bn_momentum=bn_momentum,
        )
        self.convtr7p2s2 = conv_tr(
            self.inplanes,
            self.PLANES[7],
            kernel_size=space_n_time_m(2, 1),
            upsample_stride=space_n_time_m(2, 1),
            dilation=1,
            bias=False,
            conv_type=self.NON_BLOCK_CONV_TYPE,
            D=D,
        )
        self.bntr7 = get_norm(
            self.NORM_TYPE, self.PLANES[7], D, bn_momentum=bn_momentum
        )

        self.inplanes = self.PLANES[7] + self.INIT_DIM
        self.block8 = self._make_layer(
            self.BLOCK,
            self.PLANES[7],
            self.LAYERS[7],
            dilation=dilations[7],
            norm_type=self.NORM_TYPE,
            bn_momentum=bn_momentum,
        )

        self.final = conv(
            self.PLANES[7], out_channels, kernel_size=1, stride=1, bias=True, D=D
        )
        self.relu = MinkowskiReLU(inplace=True)

    def forward(self, x):
        out = self.conv0p1s1(x)
        out = self.bn0(out)
        out_p1 = self.relu(out)

        out = self.conv1p1s2(out_p1)
        out = self.bn1(out)
        out = self.relu(out)
        out_b1p2 = self.block1(out)

        out = self.conv2p2s2(out_b1p2)
        out = self.bn2(out)
        out = self.relu(out)
        out_b2p4 = self.block2(out)

        out = self.conv3p4s2(out_b2p4)
        out = self.bn3(out)
        out = self.relu(out)
        out_b3p8 = self.block3(out)

        out = self.conv4p8s2(out_b3p8)
        out = self.bn4(out)
        out = self.relu(out)
        encoder_out = self.block4(out)

        out = self.convtr4p16s2(encoder_out)
        out = self.bntr4(out)
        out = self.relu(out)

        out = me.cat(out, out_b3p8)
        out = self.block5(out)

        out = self.convtr5p8s2(out)
        out = self.bntr5(out)
        out = self.relu(out)

        out = me.cat(out, out_b2p4)
        out = self.block6(out)

        out = self.convtr6p4s2(out)
        out = self.bntr6(out)
        out = self.relu(out)

        out = me.cat(out, out_b1p2)
        out = self.block7(out)

        out = self.convtr7p2s2(out)
        out = self.bntr7(out)
        out = self.relu(out)

        out = me.cat(out, out_p1)
        out = self.block8(out)

        out = self.final(out)

        # if self.normalize_feature:
        #     return SparseTensor(
        #         out.F / torch.norm(out.F, p=2, dim=1, keepdim=True),
        #         coordinate_map_key=out.coordinate_map_key,
        #         coordinate_manager=out.coordinate_manager,
        #     )
        # else:
        #     return out
        return out


class Res16UNet34(Res16UNetBase):
    BLOCK = BasicBlock
    LAYERS = (2, 3, 4, 6, 2, 2, 2, 2)


class Res16UNet34C(Res16UNet34):
    PLANES = (32, 64, 128, 256, 256, 128, 96, 96)


class DABlock(nn.Module):
    def __init__(
        self,
        inplanes,
        outplanes,
        # stride=1,
        # dilation=1,
        # downsample=None,
        conv_type=ConvType.HYPERCUBE,
        norm_type=NormType.BATCH_NORM,
        bn_momentum=0.1,
        D=3,
    ):
        super(DABlock, self).__init__()

        pooling_r = 4

        group_width = outplanes // 2

        def space_n_time_m(n, m):
            return n if D == 3 else [n, n, n, m]

        self.conv1_a = conv(
            inplanes,
            group_width,
            kernel_size=space_n_time_m(1, 1),
            stride=1,
            dilation=1,
            bias=False,
            conv_type=conv_type,
            D=D,
        )
        self.bn1_a = get_norm(norm_type, group_width, D, bn_momentum=bn_momentum)

        self.conv1_b = conv(
            inplanes,
            group_width,
            kernel_size=space_n_time_m(1, 1),
            stride=1,
            dilation=1,
            bias=False,
            conv_type=conv_type,
            D=D,
        )
        self.bn1_b = get_norm(norm_type, group_width, D, bn_momentum=bn_momentum)

        self.k1 = nn.Sequential(
            conv(
                group_width,
                group_width,
                kernel_size=space_n_time_m(3, 1),
                stride=1,
                dilation=1,
                bias=False,
                conv_type=conv_type,
                D=D,
            ),
            get_norm(norm_type, group_width, D, bn_momentum=bn_momentum),
        )

        self.k2 = nn.Sequential(
            MinkowskiSumPooling(kernel_size=pooling_r, stride=pooling_r, dimension=D),
            conv(
                group_width,
                group_width,
                kernel_size=space_n_time_m(3, 1),
                stride=1,
                dilation=1,
                bias=False,
                conv_type=conv_type,
                D=D,
            ),
            get_norm(norm_type, group_width, D, bn_momentum=bn_momentum),
            MinkowskiPoolingTranspose(
                kernel_size=pooling_r, stride=pooling_r, dimension=D
            ),
        )
        self.k3 = nn.Sequential(
            conv(
                group_width,
                group_width,
                kernel_size=space_n_time_m(3, 1),
                stride=1,
                dilation=1,
                bias=False,
                conv_type=conv_type,
                D=D,
            ),
            get_norm(norm_type, group_width, D, bn_momentum=bn_momentum),
        )
        self.k4 = nn.Sequential(
            conv(
                group_width,
                group_width,
                kernel_size=space_n_time_m(3, 1),
                stride=1,
                dilation=1,
                bias=False,
                conv_type=conv_type,
                D=D,
            ),
            get_norm(norm_type, group_width, D, bn_momentum=bn_momentum),
        )

        self.conv3 = conv(
            group_width * 2,
            outplanes,
            kernel_size=space_n_time_m(1, 1),
            stride=1,
            dilation=1,
            bias=False,
            conv_type=conv_type,
            D=D,
        )
        self.bn3 = get_norm(norm_type, outplanes, D, bn_momentum=bn_momentum)

        self.relu = MinkowskiReLU(inplace=True)
        self.sigmoid = MinkowskiSigmoid()

    def forward(self, x):
        residual = x

        out_a = self.conv1_a(x)
        out_a = self.bn1_a(out_a)
        out_a = self.relu(out_a)

        out_b = self.conv1_b(x)
        out_b = self.bn1_b(out_b)
        out_b = self.relu(out_b)

        out_a = self.k1(out_a)

        identity = out_b

        # sigmoid(identity + k2)
        out_b = self.sigmoid(identity + self.k2(out_b))
        out_b = self.k3(identity) * out_b  # k3 * sigmoid(identity + k2)
        out_b = self.k4(out_b)  # k4

        out_a = self.relu(out_a)
        out_b = self.relu(out_b)

        out = self.conv3(me.cat(out_a, out_b))
        out = self.bn3(out)

        out += residual
        # out = self.relu(out)

        return out


# class DABlock(nn.Module):
#     def __init__(
#         self,
#         inplanes,
#         outplanes,
#         # stride=1,
#         # dilation=1,
#         # downsample=None,
#         conv_type=ConvType.HYPERCUBE,
#         norm_type=NormType.BATCH_NORM,
#         bn_momentum=0.1,
#         D=3,
#     ):
#         super(DABlock, self).__init__()

#         def space_n_time_m(n, m):
#             return n if D == 3 else [n, n, n, m]

#         self.conv1 = conv(inplanes, outplanes, kernel_size=space_n_time_m(3, 1),
#                           stride=1, dilation=1, bias=False,
#                           conv_type=conv_type, D=D,)
#         self.bn1 = get_norm(norm_type, outplanes,
#                             D, bn_momentum=bn_momentum)

#         self.conv2 = conv(inplanes, outplanes, kernel_size=space_n_time_m(3, 1),
#                           stride=1, dilation=1, bias=False,
#                           conv_type=conv_type, D=D,)
#         self.bn2 = get_norm(norm_type, outplanes,
#                             D, bn_momentum=bn_momentum)

#         # self.final = conv(
#         #     outplanes, outplanes, kernel_size=1, stride=1, bias=True, D=D
#         # )

#         self.relu = MinkowskiReLU(inplace=True)
#         # self.sigmoid = MinkowskiSigmoid()

#     def forward(self, x):
#         residual = x

#         out = self.conv1(x)
#         out = self.bn1(out)
#         out = self.relu(out)

#         out = self.conv2(out)
#         out = self.bn2(out)

#         out += residual

#         return out


class DA(nn.Module):
    def __init__(self, in_out_channels, config, D=3):
        super(DA, self).__init__()

        self.normalize_feature = config["normalize_features"]

        bn_momentum = config["bn_momentum"]
        layers = []
        # for _ in range(3):
        #     layers.append(
        #         DABlock(in_out_channels, in_out_channels, bn_momentum=bn_momentum, D=D))
        for i in range(3):
            layers.append(
                DABlock(in_out_channels, in_out_channels, bn_momentum=bn_momentum, D=D)
            )
            # layers.append(conv(
            #     in_out_channels,
            #     in_out_channels,
            #     kernel_size=1,
            #     stride=1,
            #     dilation=1,
            #     conv_type=ConvType.SPATIAL_HYPERCUBE,
            #     D=D,
            # ))
            if i < 3 - 1:
                layers.append(MinkowskiReLU(inplace=True))

        self.module = nn.Sequential(*layers)

    def forward(self, x):
        out = self.module(x)

        if self.normalize_feature:
            return SparseTensor(
                out.F / torch.norm(out.F, p=2, dim=1, keepdim=True),
                coordinate_map_key=out.coordinate_map_key,
                coordinate_manager=out.coordinate_manager,
            )
        else:
            return out
